package com.gxa.zhonglong.service.impl;

import com.gxa.zhonglong.common.ExceptionCode;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.mapper.ProductCodeMapper;
import com.gxa.zhonglong.pojo.ProductCode;
import com.gxa.zhonglong.service.ProductCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = {Throwable.class})
public class ProductCodeServiceImpl implements ProductCodeService {
    @Autowired
    private ProductCodeMapper tbProductCodeMapper;


    @Override
    public void insert(ProductCode productCode) throws BusinessException {
        ProductCode productCode1 = tbProductCodeMapper.selectById(productCode.getProductId());
        if(productCode1!=null){
            throw new BusinessException(ExceptionCode.DUPLICATE_ROlE_NAME);
        }else{
            tbProductCodeMapper.insert(productCode);
        }
    }

    @Override
    public List<Map<String,Object>> select() {
        return tbProductCodeMapper.select();
    }

    @Override
    public List<Map<String,Object>> selectByName(String name) {
        String Name="%"+name+"%";
        return tbProductCodeMapper.selectByName(Name);
    }

    @Override
    public   ProductCode selectById(Long productId) {
     return   tbProductCodeMapper.selectById(productId);
    }

    @Override
    public void delete(Long productId) throws BusinessException {
        ProductCode productCode = tbProductCodeMapper.selectById(productId);
        if(productCode==null){
            throw new BusinessException(ExceptionCode.DELETE_DEFEATED);
        }else{
            tbProductCodeMapper.delete(productId);
        }
    }

    @Override
    public void deleteChoose(List list) {
        tbProductCodeMapper.deleteChoose(list);
    }
}
